﻿using System.Data;
using Core.Library;
using Microsoft.ApplicationBlocks.Data;

namespace DataObject.Customer
{
    public class CustomerDAL
    {
        /***************************************
        = Back End
        -------------------------------------- */        
        public static void Customer_Delete(int customerId)
        {
            SqlHelper.ExecuteScalar(Constants.ConnectionString, "Customer_Delete", customerId);
        }

        public static IDataReader Customer_GetById(int customerId)
        {
            return SqlHelper.ExecuteReader(Constants.ConnectionString, "Customer_GetById", customerId);
        }

        public static IDataReader Customer_GetByUsername(string username)
        {
            return SqlHelper.ExecuteReader(Constants.ConnectionString, "Customer_GetByUsername", username);
        }

        public static IDataReader Customer_GetByEmail(string email)
        {
            return SqlHelper.ExecuteReader(Constants.ConnectionString, "Customer_GetByEmail", email);
        }

        public static int Customer_CheckUsernameExist(string username)
        {
            return (int)SqlHelper.ExecuteScalar(Constants.ConnectionString, "Customer_CheckUsernameExist", username);
        }

        public static int Customer_CheckEmailExist(string email)
        {
            return (int)SqlHelper.ExecuteScalar(Constants.ConnectionString, "Customer_CheckEmailExist", email);
        }

        public static DataTable GetListSearch(int type, string keyword)
        {
            var filter = string.Empty;
            switch (type)
            {
                case 1: // Số điện thoại
                    filter = string.Format("Mobile LIKE N'%{0}%'", keyword);
                    break;
                case 2: // Contact Name
                    filter = string.Format("ContactName LIKE N'%{0}%'", keyword);
                    break;
                case 3: // Email
                    filter = string.Format("Email LIKE N'%{0}%'", keyword);
                    break;
            }
            return Action.DataTable_GetListPaging("Customer", "CustomerId DESC", "CustomerId, Mobile, ContactName",
                                                  filter, SettingValues.PageIndex, SettingValues.PageSize_1000);
        }

        /***************************************
        = Front End
        -------------------------------------- */
        public static int ValidateCustomer(string username, string password)
        {
            return (int)SqlHelper.ExecuteScalar(Constants.ConnectionString, "Customer_Validate", username, password);            
        }
        
        public static int Customer_Create(CustomerObject o)
        {
            return (int)SqlHelper.ExecuteScalar(Constants.ConnectionString, "Customer_Create",
                                                    o.ContactName, o.Username, o.Password, o.Mobile, o.Email, o.Address, o.IsActive);
        }

        public static void Customer_Update(int customerId, string contactName, string mobile, string email, string address)
        {
            SqlHelper.ExecuteNonQuery(Constants.ConnectionString, "Customer_Update", customerId, contactName, mobile, email, address);
        }

        public static void Customer_Update_Status(int customerId, bool isActive)
        {
            SqlHelper.ExecuteNonQuery(Constants.ConnectionString, "Customer_Update_Status", customerId, isActive);
        }        

        public static void Customer_ChangePassword(int customerId, string newPassword)
        {
            SqlHelper.ExecuteNonQuery(Constants.ConnectionString, "Customer_ChangePassword", customerId, newPassword);
        }
    }
}
